<?php


namespace App\Http\Controllers\Web;

use App\Models\Escort;
use App\Models\Order;
use App\Services\ChatService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 镖师
 * Class EscortController
 * @package App\Http\Controllers\Web
 */
class EscortController extends Controller
{
    /**
     * 镖师列表
     */
    public function escortList()
    {
        $query = $this->query;

        $escort_model = Escort::whereIn('status', [1,2])->with('leaderInfo')->orderBy('id','desc');

        if($query['realname']){
            $escort_model->where('realname',$query['realname']);
        }
        if($query['phone']){
            $escort_model->where('phone',$query['phone']);
        }

        $list = $escort_model->paginate($this->pageNum);

        return view('escort.list', ['list' => $list]);
    }

    /**
     * 申请列表
     */
    public function applyList()
    {
        $list = Escort::whereStatus(0)->paginate($this->pageNum);
        return view('escort.apply', ['list' => $list]);
    }

    /**
     * 修改状态
     */
    public function change()
    {
        $id = request()->input('id');
        $status = request()->input('status');
        $escort = Escort::find($id);

        $escort->status = $status;
        $escort->save();

        //接入客服 申请通过后添加tim账号
        if($status == 1){
            $timUid = 'ggps_escort_'.$escort->user->mobile;
            //创建客户tim用户
            ChatService::importAccount([
                "Identifier" => $timUid

            ]);
            ChatService::editAccount($timUid, ['nick' => $escort->user->mobile]);
        }

        return $this->success();
    }

    /**
     * 删除
     * @return array|string
     * @throws \Exception
     */
    public function destroy()
    {
        $id = request()->input('id');

        Escort::find($id)->delete();
        return $this->success();
    }

    /**
     * 设置成镖头
     * @return array|string
     */
    public function beLeader()
    {
        $id = request()->input('id');

        $escort = Escort::find($id);
        $escort->role = 2;
        $escort->save();

        $escort->user->is_leader = 1;
        $escort->user->save();

        return $this->success();
    }

    /**
     * 取消镖头
     * @return array|string
     */
    public function cancelLeader()
    {
        $id = request()->input('id');

        $escort = Escort::find($id);
        $escort->role = 1;
        $escort->save();

        $escort->user->is_leader = 0;
        $escort->user->save();

        return $this->success();
    }

    /**
     * 设置镖头
     */
    public function setLeader()
    {
        $uid = request()->input('uid');
        $escort_id = request()->input('escort_id');

        $escort = Escort::find($escort_id);
        $escort->leader = $uid;
        $escort->save();

        $this->success();
    }



    public function set_escort_schools(Request $request){

        $school_ids = $request->input('school_ids');
        $escort_id = $request->input('escort_id');

        $escort = Escort::find($escort_id);
        $escort->school_ids = implode(',',$school_ids);
        $escort->save();
        $this->success();

    }




    /*接单数据*/
    public function data_list(Request $request){

        $query = $this->query;

        $escort_model = Escort::whereIn('status', [1,2])->orderBy('id','desc');

        if($query['realname']){
            $escort_model->where('realname',$query['realname']);
        }
        if($query['phone']){
            $escort_model->where('phone',$query['phone']);
        }
        $list = $escort_model->paginate($this->pageNum);

        foreach ($list as $item){
            $model_count = Order::where('escort_id',$item->id)->where('status',3);
            if($query['start_time'] || $query['end_time']) {
                if ($query['start_time']) {
                    $model_count->where('escort_finish_time', '>', strtotime($query['start_time']));
                }
                if ($query['end_time']) {
                    $model_count->where('escort_finish_time', '<', strtotime($query['end_time']));
                }
            }else{
                $model_count->where('escort_finish_time', '>', strtotime(date("Y-m")));
            }
            $order_count = $model_count->count();
            $item->order_count = $order_count;
        }

        return view('escort.data_list', ['list' => $list]);



    }

    public function show_chart(Request $request){
        $escort_id = $request->input('escort_id');
        return view('escort.order_detail',['escort_id'=>$escort_id]);
    }


    public function orderchart(Request $request){
        $escort_id = $request->input('escort_id');
        $starday = strtotime('-30 day');
        $yesterday = time();

        $user_list = Order::whereBetween('created_at', [$starday, $yesterday])
            ->selectRaw("FROM_UNIXTIME(created_at,'%m-%e') as day,count(*) as count")
            ->groupBy(DB::raw("FROM_UNIXTIME(created_at, '%Y%m%e')"))
            ->orderBy('created_at', 'asc')
            ->where('status',3)
            ->where('escort_id',$escort_id)
            ->get()->toArray();
        $chartdata = [];
        $data = [];
        $chartdata['legend'] = ['订单数'];//分类
        $chartdata['yAxis']['maxnum'] = 0;//最大值数量
        $chartdata['xAxis'] = [date('m-d')];//X轴值
        $chartdata['series'] = [0];//分类1值
        if (!empty($user_list)) {
            foreach ($user_list as $k => $v) {
                $data['day'][] = $v['day'];
                $data['count'][] = $v['count'];
                if ($chartdata['yAxis']['maxnum'] < $v['count'])
                    $chartdata['yAxis']['maxnum'] = $v['count'];
            }
            $chartdata['xAxis'] = $data['day'];//X轴值
            $chartdata['series'] = $data['count'];//分类1值
        }
        return [
            'status' => 200,
            'msg' => 'ok',
            'data' => $chartdata,
        ];
    }



}
